/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import maps.DungeonMap;
import maps.DungeonOptions;

/**
 *
 * @author menderleit
 */
public class TestDungeon extends JFrame implements Runnable {

    private Canvas can;
    private BufferStrategy bs;
    private DungeonMap map;

    public TestDungeon() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Dungeon Generator");
        setVisible(true);
        Insets in = getInsets();
        setSize(1024 + in.left + in.right, 768 + in.top + in.bottom);
        setResizable(false);
        setLocationRelativeTo(null);

        can = new Canvas();
        can.setSize(1024, 768);
        can.setBackground(new Color(0, 0, 0));
        add(can);

        can.createBufferStrategy(2);
        bs = can.getBufferStrategy();
    }

    public static void main(String[] args) {
        TestDungeon app = new TestDungeon();
        Thread t = new Thread(app);
        t.start();
    }

    @Override
    public void run() {

        DungeonOptions opts = new DungeonOptions(Utils.rnd().nextInt(7) + 5, Utils.rnd().nextInt(13) + 11, Utils.rnd().nextInt(4), Utils.rnd().nextInt(5));
        map = new DungeonMap(125, 91, 8);
        map.generate(opts);

        long oldTime = System.currentTimeMillis();
        long difTime;

        long genDelay = 2500;
        long genTime = genDelay;

        while (isShowing()) {
            difTime = System.currentTimeMillis() - oldTime;
            oldTime = System.currentTimeMillis();

            genTime -= difTime;
            if (genTime <= 0) {
                genTime += genDelay;
                opts = new DungeonOptions(Utils.rnd().nextInt(7) + 5, Utils.rnd().nextInt(13) + 11, Utils.rnd().nextInt(4), Utils.rnd().nextInt(5));
                map.generate(opts);
            }

            Graphics2D g = (Graphics2D) bs.getDrawGraphics();

            g.clearRect(0, 0, 1024, 768);

            map.draw(g, 20, 20);

            g.setColor(Color.white);
            g.drawString("Next Dungeon: " + genTime, 0, 10);

            bs.show();
            g.dispose();
        }
        dispose();
        System.exit(0);
    }
}
